package cn.tinkling;

import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Platform;
import org.eclipse.core.runtime.Status;
import org.eclipse.jdt.core.IJavaElement;
import org.eclipse.jdt.core.IJavaProject;
import org.eclipse.jdt.internal.core.PackageFragmentRoot;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.ui.IEditorPart;
import org.eclipse.ui.ISelectionService;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.internal.Workbench;
import org.eclipse.ui.progress.UIJob;

@SuppressWarnings("restriction")
public final class Utils {

	private Utils() {
	}

	public static String getWorkspacePath() {
		return Platform.getLocation().toString();
	}

	public static String getCurrentProjectPath() {
		IProject project = getCurrentProject();
		if (project == null)
			return null;

		return project.getLocation().toString();
	}

	public static IProject getCurrentProject() {
		IProject project = null;

		IEditorPart editorPart = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().getActiveEditor();
		if (editorPart != null) {
			Object object = editorPart.getEditorInput().getAdapter(IFile.class);
			if (object != null) {
				project = ((IFile) object).getProject();
			}
		}

		if (project == null) {
			ISelectionService selectionService = Workbench.getInstance().getActiveWorkbenchWindow().getSelectionService();
			ISelection selection = selectionService.getSelection();
			if (selection instanceof IStructuredSelection) {
				Object element = ((IStructuredSelection) selection).getFirstElement();

				if (element instanceof IResource) {
					project = ((IResource) element).getProject();
				} else if (element instanceof PackageFragmentRoot) {
					IJavaProject jProject = ((PackageFragmentRoot) element).getJavaProject();
					project = jProject.getProject();
				} else if (element instanceof IJavaElement) {
					IJavaProject jProject = ((IJavaElement) element).getJavaProject();
					project = jProject.getProject();
				}
				// else if (element instanceof EditPart) {
				// IFile file = (IFile) ((DefaultEditDomain) ((EditPart)
				// element).getViewer().getEditDomain()).getEditorPart().getEditorInput()
				// .getAdapter(IFile.class);
				// project = file.getProject();
				// }
			}
		}

		return project;
	}

	public static void printInfoToConsoleOnUI(final String tag, final String message, final boolean activate) {
		UIJob uiJob = new UIJob("") {

			@Override
			public IStatus runInUIThread(IProgressMonitor monitor) {
				TinklingConsole.printInfo(tag, message, activate);
				return Status.OK_STATUS;
			}
		};
		uiJob.setSystem(true);
		uiJob.schedule();
	}

	public static void printErrorToConsoleOnUI(final String tag, final String message, final boolean activate) {
		UIJob uiJob = new UIJob("") {

			@Override
			public IStatus runInUIThread(IProgressMonitor monitor) {
				TinklingConsole.printErr(tag, message, activate);
				return Status.OK_STATUS;
			}
		};
		uiJob.setSystem(true);
		uiJob.schedule();
	}

}
